//
//  UIImage+Capture.m
//  HSCategoryKit
//
//  Created by huang on 2018/8/1.
//

#import "UIImage+Capture.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIImage (Capture)

#pragma mark 截图指定view成图片
+ (UIImage *)captureWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [UIScreen mainScreen].scale);
    // IOS7及其后续版本
    if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
    } else { // IOS7之前的版本
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenshot;
}

#pragma mark 截屏
+ (UIImage *)captureMainScreen
{
    UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);
    [[UIApplication sharedApplication].windows[0].layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
